﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统字典类型仓储
/// </summary>
public class SysDictTypeRep : BaseRep<SysDictTypeModel>
{
    private static string sqlFields = string.Format(@"
         `dict_id`
        ,`dict_name`
        ,`dict_type`
        ,`status`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
        ,`remark`
    ");

    /// <summary>
    /// 通过字典编号获取系统字典类型信息
    /// </summary>
    /// <param name="dictId">字典编号</param>
    /// <returns>SysDictTypeModel?</returns>
    public SysDictTypeModel? GetSysDictTypeByDictId(long dictId)
    {
        string sqlWhere = " and `dict_id` = @DictId ";

        return QuerySingle(sqlFields, sqlWhere, new { DictId = dictId });
    }

    /// <summary>
    /// 获取系统字典类型列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysDictTypeModel></returns>
    public IEnumerable<SysDictTypeModel> GetSysDictTypeList(SysDictTypeModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.DictName))
        {
            sqlWhere += " and `dict_name` like concat('%', @DictName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DictType))
        {
            sqlWhere += " and `dict_type` =  @DictType ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取系统字典类型列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysDictTypeModel></returns>
    public Page<SysDictTypeModel> GetSysDictTypePageList(SysDictTypeModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.DictName))
        {
            sqlWhere += " and `dict_name` like concat('%', @DictName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.DictType))
        {
            sqlWhere += " and `dict_type` =  @DictType ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过字典编号更新系统字典类型
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysDictTypeByDictId(SysDictTypeModel model)
    {
        string sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.DictName))
        {
            sqlSet += " `dict_name` = @DictName, ";
        }

        if (!string.IsNullOrWhiteSpace(model.DictType))
        {
            sqlSet += " `dict_type` = @DictType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `dict_id` = @DictId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过系统字典编号删除系统字典类型
    /// </summary>
    /// <param name="dictId">系统字典编号</param>
    /// <returns>int</returns>
    public int DeleteSysDictTypeByDictId(long dictId)
    {
        string sqlWhere = " and `dict_id` = @DictId ";

        return Delete(sqlWhere, new { DictId = dictId });
    }
}

